package server;

import java.io.InputStream;
import java.net.Socket;
import java.util.Map;

/**
 * @author 陶肖寅
 * @version V1.0
 * @Package server
 * @date 2020-02-24 16:15
 * @Copyright © 2020-2021 城云科技(中国)有限公司
 */
public class RequestProcessor extends Thread {
    private Socket socket;
    private Map<String,HttpServlet> servletMap;

    public RequestProcessor(Socket socket, Map<String, HttpServlet> servletMap) {
        this.socket = socket;
        this.servletMap = servletMap;
    }

    @Override
    public void run() {
        try {
            InputStream inputStream = socket.getInputStream();
            //封装Request对象
            Request request = new Request(inputStream);
            Response response = new Response(socket.getOutputStream());
            String url = request.getUrl();
            if (servletMap.get(url) == null){
                response.outputHtml(request.getUrl());
            } else{
                //请求动态资源
                HttpServlet httpServlet = servletMap.get(url);
                httpServlet.service(request,response);
            }
            socket.close();
        }catch (Exception e) {
            e.printStackTrace();
        }

    }
}
